<?php 

session_start();

if (@$_REQUEST['actionType'] == "add")
{
	@$_SESSION["profils"][$_SESSION["profilActuel"]]["inventaires"][$_REQUEST["iid"]]["bouteilles"][$_REQUEST["aid"]]["qty"] = @$_SESSION["profils"][$_SESSION["profilActuel"]]["inventaires"][$_REQUEST["iid"]]["bouteilles"][$_REQUEST["aid"]]["qty"] + $_REQUEST["quantity"];
	header('Location:inventaire.php?iid=' . $_REQUEST["iid"]);
}
elseif (@$_REQUEST['actionType'] == "remove")
{
	@$_SESSION["profils"][$_SESSION["profilActuel"]]["inventaires"][$_REQUEST["iid"]]["bouteilles"][$_REQUEST["aid"]]["qty"] = @$_SESSION["profils"][$_SESSION["profilActuel"]]["inventaires"][$_REQUEST["iid"]]["bouteilles"][$_REQUEST["aid"]]["qty"] - $_REQUEST["quantity"];
	if (@$_SESSION["profils"][$_SESSION["profilActuel"]]["inventaires"][$_REQUEST["iid"]]["bouteilles"][$_REQUEST["aid"]]["qty"] < 0)
	{
		@$_SESSION["profils"][$_SESSION["profilActuel"]]["inventaires"][$_REQUEST["iid"]]["bouteilles"][$_REQUEST["aid"]]["qty"] = 0;
	}
	header('Location:inventaire.php?iid=' . $_REQUEST["iid"]);
}
elseif(@$_REQUEST['actionType'] == "delete")
{
	unset($_SESSION["profils"][$_SESSION["profilActuel"]]["inventaires"][$_REQUEST["iid"]]["bouteilles"][$_REQUEST["aid"]]);
	$_SESSION["profils"][$_SESSION["profilActuel"]]["inventaires"][$_REQUEST["iid"]]["bouteilles"] = array_values($_SESSION["profils"][$_SESSION["profilActuel"]]["inventaires"][$_REQUEST["iid"]]["bouteilles"]);
}

@include 'include/header.php';
?>


<script type="text/javascript">
	function addBottle(aid, count)
	{
		document.getElementById("aid").value = aid;
		document.getElementById("quantity").value = document.getElementById("qty"+count).value;
		document.getElementById("actionType").value = 'add'
		form.submit();
	}
	
	function removeBottle(aid, count)
	{
		document.getElementById("aid").value = aid;
		document.getElementById("quantity").value = document.getElementById("qty"+count).value;
		document.getElementById("actionType").value = 'remove'
		form.submit();
	}
	
	function deleteBottle(aid, count)
	{
		document.getElementById("aid").value = aid;
		document.getElementById("quantity").value = document.getElementById("qty"+count).value;
		document.getElementById("actionType").value = 'delete'
		form.submit();
	}
	
</script>

<form name="form" action="inventaire.php" method="GET">

<input type="hidden" id="iid" name="iid" value="<?php echo $_REQUEST["iid"]; ?>">
<input type="hidden" id="aid" name="aid" value="">
<input type="hidden" id="quantity" name="quantity" value="">
<input type="hidden" id="actionType" name="actionType" value="">

<div id="contenu">
	<h1>
		<?php echo $_SESSION["profils"][$_SESSION["profilActuel"]]["inventaires"][$_GET["iid"]]["nom"] ?>
	</h1>
	
	<table class="inventaire" cellspacing="0" cellpadding="0">
		<tr>
			<th>Nom</th>
			<th>Type</th>
			<th>Millésime</th>
			<th>Provenance</th>
			<th>Taux d'alcool</th>
			<th>En rabais</th>
			<th>Favori</th>
			<th>Cote</th>
			<th>Qté</th>
			<th>Actions</th>
		</tr>
	<?php 
	$alcools = @$_SESSION["profils"][$_SESSION["profilActuel"]]["inventaires"][$_GET["iid"]]["bouteilles"];
	if(count($alcools) >= 1) //if sur nombre alcool
	{
		$index = 0;
		
		for ($i=0;$i<count($alcools);$i++)
		{
		
			$alcool = $alcools[$i];
			$cote = @$alcool['cote'] / 5 * 100;
			
			echo
			"<tr>
				<td>
					<a href=\"alcool.php?iid=".$_REQUEST["iid"]."&aid=".$i."\">".$alcool["nom"]."</a>
				</td>
				<td>"
					.@$alcool['type'] 
				."</td>
				<td>"
					.@$alcool['annee'] 
				."</td>
				<td>"
					.@$alcool['provenance']
				."</td>
				<td>"
					.@$alcool['tauxAlcool']
				."</td>
				<td class=\"rabais\"></td>
				<td class=\"favori\">
					<img src=\"images/favori.png\" width=\"16px\" height=\"16px\" alt=\"Favori\" /> </a>
				</td>
				
				<td>
					<div class=\"classification\">
						<div class=\"cover\"></div>
						<div class=\"progress\" style=\"width:".$cote."%;\"></div>
					</div>
				</td>
				<td>"
					.@$alcool["qty"]
				."</td>
				<td>
					<a href=\"javascript:addBottle('".$i."', '".$i."')\" title=\"Ajouter\"><img src=\"images/ajouter.png\" alt=\"Ajouter\" /></a> 
					<input id=qty".$i." type=\"text\" maxlength=\"2\" size=\"2\" value=\"1\"/> 
					<a href=\"javascript:removeBottle('".$i."', '".$i."')\" title=\"Diminuer\"><img src=\"images/enlever.png\" alt=\"Diminuer\" /></a> 
					<a href=\"javascript:deleteBottle('".$i."', '".$i."')\" title=\"Supprimer\"><img src=\"images/supprimer.png\" alt=\"Supprimer\" /></a>
				</td>
			</tr>";
		}
	}
		
	?>
	</table> 
	<p>
		<a href="ajoutAlcool.php?iid=<?php echo $_REQUEST["iid"]; ?>">Ajouter un alcool</a>
	</p>
</div>
</body>

</html>
